package com.hgw.images.service.impl;

import com.alibaba.excel.EasyExcel;
import com.hgw.images.domain.ExcelDemo;
import com.hgw.images.domain.ImageData;
import com.hgw.images.handler.CustomImageModifyHandler;
import com.hgw.images.handler.MyCellWriteHandler;
import com.hgw.images.service.DataToExcelService;
import org.springframework.stereotype.Service;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

/**
 * Description:
 *EasyExcel.write(outputStream, Excel数据实体类.class)
 *     .registerWriteHandler(new CustomImageModifyHandler()).sheet().doWrite(查询出来并且完成数据转换的Excel数据集合);
 * @author Linhuibq-YanAn
 * @date 2022/10/9 20:10
 */
@Service
public class DataToExcelServiceImpl implements DataToExcelService {

    private final String PATH = "/Users/gwh/Documents/data/demo/easy-excel-demo/images/src/main/resources/warehouse/";


    @Override
    public void download() {
        String fileName = PATH + "导出数据.xlsx";
        EasyExcel.write(fileName, ImageData.class)
                .registerWriteHandler(new CustomImageModifyHandler())
                .sheet()
                .doWrite(data());
    }

    @Override
    public void download2() {
        String fileName = PATH + "导出数据2.xlsx";
        EasyExcel.write(fileName, ExcelDemo.class)
                .registerWriteHandler(new MyCellWriteHandler())
                .sheet().doWrite(data2());
    }


    public List<ImageData> data() {
        List<ImageData> imageDataList = new ArrayList<>();
        ImageData imageData = new ImageData();
        URL url1 = null;
        URL url2 = null;
        try {
            url1 = new URL("https://img.linhuiba.com/FvXO_WtI7CI2z6-kx2BbD93fNdWS-linhuiba_watermark");
            url2 = new URL("https://img.linhuiba.com/FtwtgaPkyqlZeumvgdG6M5djaaLM-linhuiba_watermark");
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }
        imageData.setUrl(url1);
        ArrayList<URL> urls = new ArrayList<>();
        urls.add(url1);
        urls.add(url2);
        imageData.setExcelUrlList(urls);
        imageDataList.add(imageData);
        return imageDataList;
    }

    public List<ExcelDemo> data2() {
        List<ExcelDemo> imageDataList = new ArrayList<>();
        ExcelDemo imageData = new ExcelDemo();
        URL url1 = null;
        URL url2 = null;
        try {
            url1 = new URL("https://img.linhuiba.com/FvXO_WtI7CI2z6-kx2BbD93fNdWS-linhuiba_watermark");
            url2 = new URL("https://img.linhuiba.com/FtwtgaPkyqlZeumvgdG6M5djaaLM-linhuiba_watermark");
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }
        imageData.setUrl(url1);
        ArrayList<URL> urls = new ArrayList<>();
        urls.add(url1);
        urls.add(url2);
        imageData.setImageUrls(urls);
        imageDataList.add(imageData);
        return imageDataList;
    }

}
